package com.caifu.controller;

import com.caifu.bean.Result;
import com.caifu.pojo.CDevice;
import com.caifu.pojo.SysUser;
import com.caifu.service.UserCDDeptService;
import com.caifu.service.UserDeviceService;
import com.caifu.util.Utils;
import com.caifu.vo.ParamsVo;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tww
 * ClassName: DeviceController.java、
 * createTime: 2020年12月14日 14:23:06
 **/
@Controller
@RequestMapping("device")
public class DeviceController extends AbstractController {
    final static Logger logger = LoggerFactory.getLogger(DeviceController.class);

    @Resource
    private UserDeviceService userDeviceService;

    @Resource
    private UserCDDeptService userCDDeptService;

    private String pre = "view/device/";

    /**
     * 设备列表页
     */
    @GetMapping("toDeviceListPage")
    public String toDeviceListPage() {
        return pre + "device_list";
    }

    /**
     * 获取机构设备列表 分页
     */
    @GetMapping("getDeviceList")
    @ResponseBody
    public Result getDeviceList(String name, String status, Integer currentPage, Integer pageSize) {
        SysUser user = getUser();
        try {
            return success(0, "", userDeviceService.getDeviceList(user, name, status, currentPage, pageSize));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 跳机构详情页
     */
    @GetMapping("toDeviceDetailsPage")
    public String toDeviceDetailsPage(String id, Model model) {
        //获取机构详情
        CDevice cDevice = userDeviceService.getDeviceById(id);
        model.addAttribute("device", cDevice);
        model.addAttribute("id", id);
        return pre + "device_details";
    }

    /**
     * 获取可选部门
     */
    @GetMapping("getOptionalDept")
    @ResponseBody
    public Result getOptionalDept() {
        SysUser user = getUser();
        try {
            return success(0, "", userDeviceService.getOptionalDept(user.getRelativeId()));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 获取已选部门
     */
    @GetMapping("getSelectedDept")
    @ResponseBody
    public Result getSelectedDept(String id) {
        SysUser user = getUser();
        try {
            return success(0, "", userDeviceService.getSelectedDept(id, user.getRelativeId()));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }


    /**
     * 获取机构所有设备
     */
    @GetMapping("getDeviceListAll")
    @ResponseBody
    public Result getDeviceListAll() {
        SysUser user = getUser();
        try {
            return success(0, "", userDeviceService.getDeviceListAll(user));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 更新设备关联的部门
     */
    @PostMapping("updateDept")
    @ResponseBody
    public Result updateDept(ParamsVo paramsVo) {
        SysUser user = getUser();
        try {
            return userCDDeptService.updateDept(paramsVo, user);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    @Data
    @AllArgsConstructor
    public static class UserEntity {

        private Integer id;

        /**
         * 用户名
         */
        private String userName;

        /**
         * 用户手机号
         */
        private String phone;
    }

    public static void main(String args[]) {

        List<UserEntity> users = new ArrayList<>();

        users.add(new UserEntity(1, "张三", "18399990000"));
        users.add(new UserEntity(2, "王五", "18399990023"));
        users.add(new UserEntity(3, "里斯", "18399990005"));

        List<String> courseIds = users.stream().map(UserEntity::getUserName).collect(Collectors.toList());

        for (String courseId : courseIds) {
            System.out.println("courseId = " + courseId);
        }
    }


}
